<?php
include_once ('dao_log.php');
include_once ('dao_classloader.php');
include_once('dao_runtick.php');
include_once('dao_package.php');
include_once('dao_bo_session.php');
include_once('../../config/dao.cfg.php');

//define autoload function 
/*要求：
  1.处理业务的类名和消息名相同，去掉_req后缀,增加b_前缀，表示是业务对象
   如 query_task_req - > b_query_task
  2.包含类的文件文件名和类相同，严格小写
   如 query_task.php
  3:每个类可自动加载，可Unit test
  4:每个类在被调用时创建，然后被缓存。不保留任何中间信息，如需保存任何中间信息，另外设计
  5:每个类必须实现execute(&$dao_req,&$qscResp)方法,返回0表示成功，其他失败，所以不应用!ret判断成败;
*/

/*
//error_reporting(FATAL | ERROR | WARNING);
function __autoload($class) {
	require(strtolower($class) . '.php'); 
} 
*/
function load_class($type)
{
	//error_reporting(E_ALL);
	$class='b_'.$type;
	if(!class_exists($class))
	{
		$file = $class.'.php';
		//DAO_Log::debug('try include file $file');
		//include_once($file);
		if(include_file($file)<0)
		{
			return NULL;
		}
	}

	if(!class_exists($class))
	{
		return NULL;
	}
	
	$object  = new $class();
	if(!isset($object) )
	{
		DAO_Log::error('Load Class error : '.$type);
		return $object;
	}
	
	//$classCache[$type]=$object;
//   / DAO_Log::debug('Load Class success:'.get_class($object));
	return $object;
}

//----------------------------
function daobusiness(&$dao_req,&$dao_resp)
{
	$ret = 0;
	
	$msg='OK';
	$pos = strpos($dao_req->type,'_req');
	Run_Tick::pick('bizStart');
	if(!$pos||$pos===false)
	{
		//没有_req后缀
		//$msg='maybe Wrong request,withou _req:'.$dao_req->type;
		//DAO_Log::debug($msg);
	}

    $dao_req->decode();
	if($pos>0)
	{
		$type = substr($dao_req->type,0,$pos);
	}
	else
	{
		$type = $dao_req->type;
	}

	if(strlen(trim($type))==0)
	{
		//错误的格式
		$msg='Wrong request format:'.$dao_req->type;
		$type = 'task';
		$ret = -1;
	}

   	bo_session_set('request', $type);
	$resp_type = $type.'_resp';    
	if(!isset($dao_resp) )
	{
		$dao_resp = new DAO_Package();
	}
	if($dao_req->format=='XML'||$dao_req->format=='SOAP')
	{
		DAO_Log::debug("$type,raw Request=\r\n----------\r\n".bo_session_get('reqxml')."\r\n------------\r\n");
	}
	if($ret==0)
	{
			//加载处理器
			Run_Tick::pick('startLoadClas');
			$biz_object = load_class($type);
			Run_Tick::pick('stopLoadClas');
			if(!isset($biz_object))
			{
				$msg = 'cannot found the biz object, not implement or missing:'.$type;
				$ret = -2;
			}
			Run_Tick::pick($type.'_execute_start');
			if(isset($biz_object))
			{
				$ret = $biz_object->execute($dao_req,$dao_resp,$msg);
				if(!isset($ret))$ret=0;
			}
			Run_Tick::pick($type.'_execute_end');
	}
	if($ret!=0)
	{
          DAO_Log::error("daobussiness_error:$type ,$ret,$msg,\r\nraw Request=\r\n".bo_session_get('reqxml')."\r\n-----------
-\r\n");
	}
  
	$dao_resp->type=$resp_type;
	$dao_resp->header['retcode']=$ret;
	$dao_resp->header['reason']=$msg;
    //------------------------------------
    //如果请求包记录了client_charset,则以其为准
    //目前只有因为xml解析的特殊原因（php必须用utf-8编码），才可能会有charset和client_charset不同的情况
    if($dao_req->header['client_charset'])
    {
        $dao_resp->header['charset']=$dao_req->header['client_charset'];
    }

    $dao_resp->encode();
	DAO_Log::debug('after business,resp.recordcount='.sizeof($dao_resp->data));
	return $ret;
}

?>